package qianfeng.com.meijiadaka;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.DefaultSliderView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.lemon.view.RefreshRecyclerView;
import cn.lemon.view.adapter.Action;
import qianfeng.com.meijiadaka.adapter.gnk.GnkAdapter;
import qianfeng.com.meijiadaka.bean.gnk.GnkBean;
import qianfeng.com.meijiadaka.bean.gnk.HeaderBean;
import qianfeng.com.meijiadaka.interfacebean.gnk.GnkData;
import qianfeng.com.meijiadaka.interfacebean.gnk.HeaderData;
import qianfeng.com.meijiadaka.myview.RecycleViewDivider;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

//涨姿势
public class GnkActivity extends AppCompatActivity {

    @BindView(R.id.activity_gnk)
    LinearLayout mActivityGnk;
    private String mHeader = "http://home.naildaka.com/vapi/v3/home/knowledge";
    private String mUri = "http://home.naildaka.com/vapi/v3/home/knowledge/articles?pageNo=1";

    @BindView(R.id.gnk_ib)
    ImageButton mGnkIb;
    @BindView(R.id.gnk_rrv)
    RefreshRecyclerView mRecyclerView;
    private GnkAdapter mGnkAdapter;
    private SliderLayout mSliderLayout;
    private ArrayList<ImageView> mList;
    private ImageView mIv1;
    private ImageView mIv2;
    private ImageView mIv3;
    private ImageView mIv4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gnk);
        ButterKnife.bind(this);
        init();

        loadHeaderData();
        loadData();
    }

    private void loadData() {
        new Retrofit.Builder().baseUrl("http://home.naildaka.com/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(GnkData.class).
                getData(mUri).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Action1<GnkBean>() {
                    @Override
                    public void call(GnkBean gnkBean) {
                        mGnkAdapter.addAll(gnkBean.getResult());
                        mRecyclerView.dismissSwipeRefresh();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Toast.makeText(GnkActivity.this,"网络不好",Toast.LENGTH_SHORT).show();
                    }
                });

    }

    private void init() {
        mList = new ArrayList<>();
        mGnkAdapter = new GnkAdapter(this);
        mRecyclerView.setSwipeRefreshColors(0xFF437845, 0xFFE44F98, 0xFF2FAC21);
        mRecyclerView.setLayoutManager(new LinearLayoutManager((this)));
        mRecyclerView.setAdapter(mGnkAdapter);
        addHeader();
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this,LinearLayout.HORIZONTAL,5, Color.WHITE));
        mRecyclerView.setRefreshAction(new Action() {
            @Override
            public void onAction() {
                mGnkAdapter.clear();
                loadData();
            }
        });
    }

    private void addHeader() {
        View view = LayoutInflater.from(this).inflate(R.layout.haeder_gnktiem, mActivityGnk,false);
        mSliderLayout = (SliderLayout) view.findViewById(R.id.header_gnk_sl);
        mIv1 = (ImageView) view.findViewById(R.id.header_gnk_iv1);
        mIv2 = (ImageView) view.findViewById(R.id.header_gnk_iv2);
        mIv3 = (ImageView) view.findViewById(R.id.header_gnk_iv3);
        mIv4 = (ImageView) view.findViewById(R.id.header_gnk_iv4);
        mList.add(mIv1);
        mList.add(mIv2);
        mList.add(mIv3);
        mList.add(mIv4);
        mGnkAdapter.setHeader(view);
    }

    private void loadHeaderData() {
        new Retrofit.Builder().baseUrl("http://home.naildaka.com/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).build().
                create(HeaderData.class).
                getData(mHeader).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<HeaderBean>() {
            @Override
            public void call(HeaderBean headerBean) {
                HeaderBean.ResultBean resultBean = headerBean.getResult();
                final List<HeaderBean.ResultBean.ChosenBean> chosen = resultBean.getChosen();
                        for (int i = 0; i < chosen.size(); i++) {
                            Glide.with(GnkActivity.this).load(chosen.get(i).getCategoryIcon()).into(mList.get(i));
                            final int finalI = i;
                            mList.get(i).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent=new Intent(GnkActivity.this,JingxuanActivity.class);
                                    intent.putExtra("id",chosen.get(finalI).getCategoryId());
                                    startActivity(intent);

                                }
                            });
                        }
                List<HeaderBean.ResultBean.HeadlineBean> headline = resultBean.getHeadline();
                for (int i = 0; i < headline.size(); i++) {
                            DefaultSliderView s=new DefaultSliderView(GnkActivity.this);
                            s.image(headline.get(i).getArticlePicture());
                            s.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                                @Override
                                public void onSliderClick(BaseSliderView slider) {
                                    Toast.makeText(GnkActivity.this,"dianjie",Toast.LENGTH_SHORT).show();
                                }
                            });
                            mSliderLayout.addSlider(s);
                        }

            }

        }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.e("wang",throwable.toString());
                        Toast.makeText(GnkActivity.this,"网络不好22",Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @OnClick(R.id.gnk_ib)
    public void onClick() {
        finish();
    }
}
